package com.smalls.redshoes.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager.LayoutParams;

public class CustomDialog extends Dialog {
    private int height = -1;
    private KeyEventDispacher keyEventDispacher;
    private int posX;
    private int posY;
    private View rootView;
    private int width = -1;

    public interface KeyEventDispacher {
        boolean dispatchKeyEvent(KeyEvent keyEvent);
    }

    private CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(1);
        setContentView(this.rootView);
        getWindow().setLayout(this.width, this.height);
    }

    public static CustomDialog createAndShowDialog(Context context, int style, View rootView) {
        CustomDialog dialog = new CustomDialog(context, style);
        dialog.getWindow().setGravity(81);
        dialog.getWindow().setGravity(17);
        dialog.getWindow().setFlags(ViewCompat.MEASURED_STATE_TOO_SMALL, ViewCompat.MEASURED_STATE_TOO_SMALL);
        dialog.setRootView(rootView);
        dialog.show();
        return dialog;
    }

    public static CustomDialog createAndShowDialog(Context context, int style, View rootView, int width, int height) {
        CustomDialog dialog = new CustomDialog(context, style);
        dialog.getWindow().setGravity(81);
        dialog.getWindow().setGravity(17);
        dialog.getWindow().setFlags(ViewCompat.MEASURED_STATE_TOO_SMALL, ViewCompat.MEASURED_STATE_TOO_SMALL);
        dialog.setWidth(width);
        dialog.setHeight(height);
        dialog.setRootView(rootView);
        dialog.getWindow().setLayout(width, height);
        dialog.show();
        return dialog;
    }

    public static CustomDialog createAndShowDialog(Context context, int style, View rootView, int dialogType) {
        CustomDialog dialog = new CustomDialog(context, style);
        dialog.getWindow().setGravity(81);
        dialog.getWindow().setGravity(17);
        dialog.getWindow().setType(dialogType);
        dialog.setDialogUnFocus();
        dialog.getWindow().setFlags(ViewCompat.MEASURED_STATE_TOO_SMALL, ViewCompat.MEASURED_STATE_TOO_SMALL);
        dialog.setRootView(rootView);
        dialog.show();
        return dialog;
    }

    public static CustomDialog createDialog(Context context, int style, int width, int height) {
        CustomDialog dialog = new CustomDialog(context, style);
        dialog.getWindow().setFlags(ViewCompat.MEASURED_STATE_TOO_SMALL, ViewCompat.MEASURED_STATE_TOO_SMALL);
        dialog.setWidth(width);
        dialog.setHeight(height);
        dialog.getWindow().setLayout(width, height);
        return dialog;
    }

    public static CustomDialog createDialog(Context context, int style) {
        CustomDialog dialog = new CustomDialog(context, style);
        dialog.getWindow().setType(2008);
        dialog.getWindow().setFlags(ViewCompat.MEASURED_STATE_TOO_SMALL, ViewCompat.MEASURED_STATE_TOO_SMALL);
        return dialog;
    }

    public CustomDialog setGravity(int gravity) {
        getWindow().setGravity(gravity);
        return this;
    }

    public CustomDialog setRootView(View rootView) {
        this.rootView = rootView;
        return this;
    }

    public CustomDialog setDialogCancleble(boolean cancleble) {
        setCancelable(cancleble);
        return this;
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        if (this.keyEventDispacher == null || !this.keyEventDispacher.dispatchKeyEvent(event)) {
            return super.dispatchKeyEvent(event);
        }
        return true;
    }

    public int getPosX() {
        return this.posX;
    }

    public void setPosX(int posX) {
        this.posX = posX;
    }

    public View getRootView() {
        return this.rootView;
    }

    public int getPosY() {
        return this.posY;
    }

    public void setPosY(int posY) {
        this.posY = posY;
    }

    public int getWidth() {
        return this.width;
    }

    public void setWidth(int width) {
        if (width > 0) {
            this.width = width;
        }
    }

    public int getHeight() {
        return this.height;
    }

    public void setHeight(int height) {
        if (height > 0) {
            this.height = height;
        }
    }

    public void setKeyEventDispacher(KeyEventDispacher keyEventDispacher) {
        this.keyEventDispacher = keyEventDispacher;
    }

    public void setDialogUnFocus() {
        LayoutParams lp = new LayoutParams(8);
        lp.flags = 40;
        getWindow().setAttributes(lp);
    }
}
